<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #deatou{border: 1px dashed #eee;width: 100px;height: 100px;background-color: rgba(240, 247, 255, 1);}
    #deatou img{width: 100%;height: 100%;display: none}
    #upload_avatar{color: #0E0EFF;cursor: pointer;}
    .deatou_exp{margin: 10px 0}
    #uploadfile{display: none}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>新建公司资讯</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="title" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>资讯分类</label>
                        <div class="layui-input-block">
                            <div id="newstype"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发布时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="dt" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发布人</label>
                        <div class="layui-input-block">
                            <input type="text" id="faer" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">发布部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="fadept" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>封面图片</label>
                        <div class="layui-input-block">
                            <div id="deatou">
                                <input type="hidden" id="img" name="img">
                                <input type="hidden" id="img_id" name="img_id">
                                <img id="avatar_dea" src="">
                            </div>
                            <div class="deatou_exp">
                                <input type="file" accept="image/*" id="uploadfile" value="上传头像" />
                                <span id="upload_avatar">上传图片</span>
                                <span>（请上传jpg或png文件,文件大小不超过1M）</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea"><text>*</text>摘要(200字以内)</label>
                        <div class="layui-input-block">
                            <textarea id="zhai" name="zhai" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea"><text>*</text>内容</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>


    $("#commentForm").validate({
        rules: {
            title: {required: true},
            zhai: {required: true},
            explain: {required: true},
        },
        messages:{
            title: {required: "请输入"},
            zhai: {required: "请输入"},
            explain: {required: "请输入"},
        }
    });



    var jietype;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_news_add}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#dt").val(data.dt);
                $("#faer").val(data.faer);
                $("#fadept").val(data.fadept);

                xmselect_dan('newstype', res.data.newstype, '', function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#newstype").find('xm-select').removeClass('error');
                    }else{
                        $("#newstype").find('xm-select').addClass('error');
                    }
                });

                render_from();
            }
        });

        $("#upload_avatar").click(function () {
            $("#uploadfile").click();
        });

        $("#uploadfile").change(function () {
            var loading = layui.layer.load(0, {shade: 0.1});
            let fd = new FormData();
            fd.append('file', this.files[0]);
            fd.append('token', facade.getCookie('token'));
            $.ajax({
                url: server_api_name+ask_upload_avatar,
                type: "POST",
                data: fd,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function (res) {
                    layui.layer.close(loading);
                    if (res.code === 2000) {
                        $("#img_id").val(res.data.id);
                        $("#img").val(res.data.encpath);
                        $("#avatar_dea").attr('src',get_file_complete(res.data.encpath)).show();
                    }
                },
                error: function (res) {
                    layui.layer.close(loading);
                    facade.error("网络错误");
                },
            })
        });

        form.on('submit(add)', function(data){


            var validRes = $("#commentForm").valid();
            if(!data.field.newstype){
                $("#newstype").find('xm-select').addClass('error');
                validRes = false;
            }else{
                $("#newstype").find('xm-select').removeClass('error');
            }
            if(!validRes){return false;}


            facade.ajax({url: ask_news_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });


</script>
</body>
</html>
